
package com.canyinghao.cananimation.demo;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.DragInfo;
import ohos.agp.components.Image;
import ohos.agp.components.PageFlipper;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.multimodalinput.event.TouchEvent;

import java.util.Optional;

/**
 * @ProjectName: CanAnimation
 * @ClassName: FlipperDemoAbility
 * @Description: 预览动画
 * @Author: lr
 * @CreateDate: 2021/2/24 10:34
 */
public class FlipperDemoAbility extends Ability implements Component.TouchEventListener, Component.DraggedListener {
    static final HiLogLabel logLabel = new HiLogLabel(HiLog.LOG_APP, 0x00101, "FlipperDemoAbility");
    PageFlipper viewFlipper;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        super.setUIContent(ResourceTable.Layout_ability_flipper_demo);
        viewFlipper = (PageFlipper) findComponentById(ResourceTable.Id_vf);
        viewFlipper.setTouchEventListener(this::onTouchEvent);
        viewFlipper.setDraggedListener(Component.DRAG_HORIZONTAL_VERTICAL, this);
        viewFlipper.setFlipPeriod(3000);
        viewFlipper.showNext();
        viewFlipper.showPrevious();

        AnimatorProperty lInAnim = viewFlipper.createAnimatorProperty();
        lInAnim.moveFromX(getDisplayWidth()).moveToX(0).alphaFrom(0.1f).alpha(1.0f).setDuration(1500);
        AnimatorProperty lOutAnim = viewFlipper.createAnimatorProperty();
        lOutAnim.moveFromX(0).moveToX(-getDisplayWidth()).alphaFrom(1.0f).alpha(0.1f).setDuration(1500);

        viewFlipper.setIncomingAnimation(lInAnim);
        viewFlipper.setOutgoingAnimation(lOutAnim);

        if (!viewFlipper.isFlipping()) {
            viewFlipper.startFlipping();
        }
    }

  private int getDisplayWidth() {
        int width = 0;
        DisplayAttributes displayAttributes;
        Optional<Display> optionalDisplay = DisplayManager.getInstance().getDefaultDisplay(this);
        if (optionalDisplay.isPresent()) {
            displayAttributes = optionalDisplay.get().getAttributes();
            width = displayAttributes.width;
        }
        return width;
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
      //  viewFlipper.stopFlipping();             // 点击事件后，停止自动播放
        return true;
    }

    @Override
    public void onDragDown(Component component, DragInfo dragInfo) {
        HiLog.debug(logLabel, "start:" + dragInfo.startPoint.getPointX() + "+++++updata:" + dragInfo.updatePoint.getPointX() + "=============down:" + dragInfo.updatePoint.getPointX());
    }

    @Override
    public void onDragStart(Component component, DragInfo dragInfo) {

    }

    @Override
    public void onDragUpdate(Component component, DragInfo dragInfo) {

    }

    @Override
    public void onDragEnd(Component component, DragInfo dragInfo) {

    }

    @Override
    public void onDragCancel(Component component, DragInfo dragInfo) {

    }
}
